
const { creteParagraph,findParagraphs,delParagraph,updateParagraph } = require('../model/paragraphMode')
//新增
const addParagraph=async (req,res,next)=>{
    req.body.content=JSON.parse(req.body.content)
    const result=await creteParagraph(req.body)
    console.log(req.body)
    if(result){
        res.send({httpStatus:200,data:result,msg:'增加成功'})
    }else{
        res.send({httpStatus:400,data:[],msg:'增加出错'})
    }
}
//查询
const getParagraphs = async (req, res) => {
    const result = await findParagraphs(req.query);
    if (result.length||result.length===0) {
        res.send({ httpStatus: 200, total: result.length, data: result })
    } else {
        res.send({ httpStatus: 400,  msg: '查询出错',date:[]})
    }
}

//删除
const deleteParagraph = async (req, res) => {
    const { _id } = req.query;
    const query = { _id };
    const result = await delParagraph(query)
    if (result.n) {
        res.send({ httpStatus: 200, state: true, msg: '删除成功' })
    } else {
        res.send({ httpStatus: 400, state: false, msg: '删除出错' })
    }
}
//修改
const editParagraph = async (req, res) => {
    const { _id,content } = req.body;
    const result = await updateParagraph({_id}, JSON.parse(content))
    if (result.nModified !== 0) {
        res.send({ httpStatus: 200,  msg: '修改成功' })
    } else {
        res.send({ httpStatus: 400,  msg: '修改出错' })
    }

}

module.exports = {
    addParagraph,
    getParagraphs,
    deleteParagraph,
    editParagraph
}